package com.edushare.common.dao;
import java.util.HashMap;
import java.util.List;

import org.apache.cayenne.DataObjectUtils;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.query.SelectQuery;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;


public class UniversitiesDAO extends CayenneBasedDAO {
	public UniversitiesDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}
	
	@SuppressWarnings("unchecked")
	public List<Universities> getAllUniversities(){
		SelectQuery query = new SelectQuery(Universities.class);
		List<Universities> lst = getDataContext().performQuery(query);
		return lst;
	}
	
	
	public Universities getUnivById(int pk){
		return DataObjectUtils.objectForPK(getDataContext(), Universities.class, pk);
	}
	
	@SuppressWarnings("unchecked")
	public Universities getUnivById(String uniqueName){
		Expression exp = Expression.fromString("uniqueName = $uniquename");
		HashMap hm = new HashMap();
		hm.put("uniquename", uniqueName);
		SelectQuery query = new SelectQuery(Universities.class, exp.expWithParameters(hm));
		List lst = getDataContext().performQuery(query);
		if(lst.size()>0)
			return (Universities) lst.get(0);
		else 
			return null;
	}

	@SuppressWarnings("unchecked")
	public boolean isUniqueNameAvailable(String input) {
		Expression exp = Expression.fromString("uniqueName = $uniquename");
		HashMap hm = new HashMap();
		hm.put("uniquename", input);
		SelectQuery query = new SelectQuery(Universities.class, exp.expWithParameters(hm));
		List lst = getDataContext().performQuery(query);
		if(lst!=null && lst.size() > 0)
			return false;
		
		if(new LookupDAO(ormInterface).isIllegalInput(input))
			return false;
		
		return true;
	}

	@SuppressWarnings("unchecked")
	public boolean isUniversityNameUnique(String univNameStr) {
		Expression exp = Expression.fromString("universityName = $univname");
		HashMap hm = new HashMap();
		hm.put("univname", univNameStr);
		SelectQuery query = new SelectQuery(Universities.class, exp.expWithParameters(hm));
		List lst = getDataContext().performQuery(query);
		if(lst!=null && lst.size() > 0)
			return false;
		return true;
	}

	public HashMap<String, String> registerStudentById(Universities univ, String userList) {
		HashMap<String, String> userPassword = new HashMap<String, String>();
		if(!StringUtil.isValid(userList))
			return userPassword;
		List<String> userNames = StringUtil.csvToList(userList);
		for(String uname: userNames){
			if(!StringUtil.isValid(uname))
				continue;
			UsersDAO udao = new UsersDAO(ormInterface);
			if(udao.getUserByUserNameOrEmail(uname)==null){
				uname = uname.trim();
				String password = uname + (int)(Math.random()*1000);
				userPassword.put(uname, password);
				Users u = getDataContext().newObject(Users.class);
				u.setUserName(uname);
				u.setPassword(password);
				u.setStatus(Users.ACTIVE);
				u.setLandingPageId(Users.LANDING_PAGE_USER_HOME);
				UserRoles ur = new UserRolesDAO(ormInterface).addUser(univ, u, UserRoles.STUDENT_ROLE,"");
				ur.setStatus(UserRoles.STATUS_ACTIVE);
				getDataContext().commitChanges();
				
			}else{
				userPassword.put(uname, "Can't create new user as this user already exists");
			}
		}
		return userPassword;
	}

}
